package com.clw.template.admin.controller;


import com.clw.template.dto.YhAdParam;
import com.clw.template.entity.YhAd;
import com.clw.template.service.IYhAdService;
import com.clw.template.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author clw
 * @since 2019-11-23
 */
@Api(tags = "YhAdController",description = "广告管理")
@RestController
@RequestMapping("/admin/ad")
public class YhAdController {
    @Autowired
    private IYhAdService adService;

    @PreAuthorize("hasAuthority('promotion:ad:list')")
    @ApiOperation("查询广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "分页页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "分页条数",dataType = "Integer",paramType = "query")
    })
    @GetMapping("list")
    public CommonResult list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        return CommonResult.success(adService.adminList(pageNum,pageSize));
    }

    @PreAuthorize("hasAuthority('promotion:ad:getById')")
    @ApiOperation("获取指定广告")
    @GetMapping("{id}")
    public CommonResult<YhAd> getById(@PathVariable Long id){
        return CommonResult.success(adService.getById(id));
    }

    @PreAuthorize("hasAuthority('promotion:ad:add')")
    @ApiOperation("添加广告")
    @PostMapping("add")
    public CommonResult add(@Validated @RequestBody YhAdParam adParam){
        YhAd ad = new YhAd();
        BeanUtils.copyProperties(adParam,ad);
        adService.add(ad);
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('promotion:ad:deleted')")
    @ApiOperation("删除广告")
    @DeleteMapping("delete")
    public CommonResult deleted(@RequestParam List<Long> ids){
        adService.deleted(ids);
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('promotion:ad:edit')")
    @ApiOperation("修改广告")
    @PutMapping("{id}")
    public CommonResult edit(@PathVariable Long id,@Validated @RequestBody YhAdParam adParam){
        YhAd ad = new YhAd();
        BeanUtils.copyProperties(adParam,ad);
        ad.setId(id);
        adService.edit(ad);
        return CommonResult.success();
    }
}
